package com.hxd.mall.utils.open

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

/**
 * 倒计时
 */
object CountDownTimerUtil {
    /**
     * 倒计时开始
     * @param futureTime 总秒数
     */
    fun startTimer(futureTime: Long, finished: () -> Unit, onTick: (time: Long) -> Unit): Disposable {
        return Observable.interval(0, 1, TimeUnit.SECONDS).take(futureTime + 1).subscribe {
            AndroidSchedulers.mainThread().scheduleDirect {
                if (0L == (futureTime - it)) {
                    finished()
                } else {
                    onTick(futureTime - it)
                }
            }
        }
//        val mCountDownTimer = object : CountDownTimer(futureTime * 1000, 1000) {
//            override fun onFinish() {
//                finished()
//            }
//
//            override fun onTick(p0: Long) {
////                if (p0 == 0L) {
////                    return
////                }
//                onTick(p0)
//            }
//        }
//        mCountDownTimer.start()
//        return mCountDownTimer
    }
}